package com.dnake.panel;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dnake.Constant;
import com.dnake.entity.EventBusTimeBean;
import com.dnake.entity.EventSettingBean;
import com.dnake.fragment.AdminFragment;
import com.dnake.fragment.FaceScoreFragment;
import com.dnake.fragment.LockTypeFragment;
import com.dnake.fragment.VoiceFragment;
import com.dnake.fragment.VoipPhoneFragment;
import com.dnake.fragment.SettingFragment;
import com.dnake.fragment.SyncFragment;
import com.dnake.misc.Sound;
import com.dnake.misc.SysTalk;
import com.dnake.misc.SysWifi;
import com.dnake.utils.DateUtil;
import com.dnake.utils.NetUtil;
import com.dnake.utils.ToastUtil;
import com.dnake.v700.sys;
import com.wx.android.common.util.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;


/**
 * @author haoyuliu
 * @since 2017/7/18
 */

public class SettingActivity extends BaseActivity {

    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.tv_network)
    TextView tvNetwork;
    @Bind(R.id.iv_signal)
    ImageView ivSignal;
    @Bind(R.id.ll_gsm)
    LinearLayout llGsm;
    @Bind(R.id.iv_status)
    ImageView ivStatus;
    @Bind(R.id.tv_status)
    TextView tvStatus;
    @Bind(R.id.ll_wifi)
    LinearLayout llWifi;
    @Bind(R.id.fl_container)
    FrameLayout flContainer;
    private ScanFaceTimer mScanFaceTimer;

    private int mLanCarrier=0;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting_new;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        AdminFragment fragment = findFragment(AdminFragment.class);
        if (fragment == null) {
            loadRootFragment(R.id.fl_container, AdminFragment.newInstance());
        }
        tvDate.setText(DateUtil.currentDatetime() + " " + DateUtil.getCurrentWeek());
        if ("2".equals(SharedPreferencesUtils.getString(Constant.SP_DEVICE_TYPE))){
            tvAddress.setText(SharedPreferencesUtils.getString(Constant.SP_COMMUNITYNAME)
                    + SharedPreferencesUtils.getString(Constant.SP_COMBINNAME));
        }else if ("1".equals(SharedPreferencesUtils.getString(Constant.SP_DEVICE_TYPE))){
            tvAddress.setText(SharedPreferencesUtils.getString(Constant.SP_COMMUNITYNAME)
                    +"  "+ SharedPreferencesUtils.getString(Constant.SP_COMBINNAME));
        }

        //20秒后无操作自动关闭设置页面
        mScanFaceTimer = new ScanFaceTimer(20000, 1000);
        mScanFaceTimer.start();
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        Sound.key_0_9 = false;
    }

    @Override
    public void onTimer() {
        super.onTimer();
        if (SysTalk.netState){
            llWifi.setVisibility(View.VISIBLE);
            llGsm.setVisibility(View.GONE);
            tvStatus.setText("网络正常");
            ivStatus.setImageResource(R.drawable.icon_normal);
        }else {
            llWifi.setVisibility(View.VISIBLE);
            llGsm.setVisibility(View.GONE);
            ivStatus.setImageResource(R.drawable.icon_wrong);
            tvStatus.setText("网络中断");
        }
//        //网络状态
//        if (mLanCarrier != sys.lan_carrier()) {
//            mLanCarrier = sys.lan_carrier();
//            if (mLanCarrier == 0) {
//                if (SysWifi.isConnected())
//                    mLanCarrier = 1;
//            }
//            if (mLanCarrier==0){
//                llWifi.setVisibility(View.VISIBLE);
//                llGsm.setVisibility(View.GONE);
//                ivStatus.setImageResource(R.drawable.icon_wrong);
//                tvStatus.setText("网络中断");
//            }else {
//                llWifi.setVisibility(View.VISIBLE);
//                llGsm.setVisibility(View.GONE);
//                tvStatus.setText("网络正常");
//                ivStatus.setImageResource(R.drawable.icon_normal);
//            }
//        }else if (mLanCarrier==1){
//            llWifi.setVisibility(View.VISIBLE);
//            llGsm.setVisibility(View.GONE);
//            tvStatus.setText("网络正常");
//            ivStatus.setImageResource(R.drawable.icon_normal);
//        }
    }
    @Override
    public void onKey(String key) {
        super.onKey(key);
        if (key.charAt(0)=='*'){
            if (!isFinishing())
                onBackPressed();
        }
        if (key.charAt(0)=='1'){
            if (getTopFragment() instanceof VoiceFragment){
                EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_VOICE_SET,  "1"));
            }
        }
        if (key.charAt(0)=='7'){
            if (getTopFragment() instanceof VoiceFragment){
                EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_VOICE_SET,  "7"));
            }
        }
        if (key.charAt(0) == '2') {
            if (getTopFragment() instanceof LockTypeFragment){
                EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_LOCK_TYPE, "up"));
            }else if (getTopFragment() instanceof FaceScoreFragment){
                EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_FACE_SET,  "up"));
            }else if (getTopFragment() instanceof VoiceFragment){
                EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_VOICE_SET,  "2"));
            }
        }
        if (key.charAt(0)=='8' ) {
            if (getTopFragment() instanceof LockTypeFragment){
                EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_LOCK_TYPE, "down"));
            }else if (getTopFragment() instanceof FaceScoreFragment){
                EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_FACE_SET,  "down"));
            }else if (getTopFragment() instanceof VoiceFragment){
                EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_VOICE_SET,  "8"));
            }
        }
        if (key.charAt(0)=='4') {
            if (getTopFragment() instanceof FaceScoreFragment) {
                EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_FACE_SET, "4"));
            }
        }
    }

    @Override
    protected void keyData(String data) {
        super.keyData(data);
        if (getTopFragment() instanceof AdminFragment) {
            EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_ADMIN, data));
            if (data.length() == 8) {
                if (Constant.ADMIN_PWD.equals(data)) {
                    mScanFaceTimer.cancel();
                    stringBuffer.setLength(0);
                    replaceFragment(SettingFragment.newInstance(), false);
                } else {
                    stringBuffer.setLength(0);
                    ToastUtil.showFailView("密码错误");
                }
            }
        } else if (getTopFragment() instanceof SettingFragment) {
            EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_SETTING, data));
            stringBuffer.setLength(0);
        } else if (getTopFragment() instanceof SyncFragment) {
            EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_SYNC, data));
            stringBuffer.setLength(0);
        } else if (getTopFragment() instanceof VoipPhoneFragment) {
            EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_REBOOT, data));
            stringBuffer.setLength(0);
        } else if (getTopFragment() instanceof LockTypeFragment) {
            EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_LOCK_TYPE, data));
            stringBuffer.setLength(0);
        }else if (getTopFragment() instanceof FaceScoreFragment){
            EventBus.getDefault().post(new EventSettingBean(Constant.EVENTBUS_FACE_SET, data));
            stringBuffer.setLength(0);
        }
    }

    @Override
    public void onBackPressedSupport() {
        if (!isFinishing())
            super.onBackPressedSupport();
    }

    /**
     * 更新时间
     *
     * @param eventBusTimeBean
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventTime(EventBusTimeBean eventBusTimeBean) {
        tvDate.setText(DateUtil.currentDatetime() + " " + DateUtil.getCurrentWeek());
    }


    class ScanFaceTimer extends CountDownTimer {

        public ScanFaceTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            finish();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}
